function _form_base_show(){
	var _this = this;
	
	if (this.is_loaded == true){
		_form_helper_show(this);
		return;
	}
	
	//LOAD THE CONTENT OF THE PAGE
	var request = {
			  ajax_url		: this.content_url
			, raw_data		: true
		};
	
	get_server_data(request, $.proxy(function(data){
		var div = $("<div id='"+ this.context.substring(1) +"'>")
					.html(data)
					.addClass("middle_panel")
					.addClass("ui-widget");
		$(document.body).append(div);
		_form_helper_show(this);
		this.is_loaded = true;
	}, this));
}

function _form_helper_show(_this){
	$(".left_panel_more").hide();
	//NOW HIDE ALL THE PANELS 
	for(var key in panel_list){
		if (typeof panel_list[key].hide == "function")
			if (panel_list[key].context != _this.context)
				panel_list[key].hide();
	}

	$(_this.context).find(".label").each(function(){
		$(this).css({
			  verticalAlign	: "top"
			, width			: "200px"
		});
	});

	$(_this.context).find("table td").each(function(){
		$(this).css({
			  "padding-bottom"	: "5px"
		});
	});

	//SET-UP THE DATE PICKERS
	$(_this.context).find(".form-date-field").each(function(){
		var date = $.format.date((new Date()), "dd MMMM yyyy")
		
		$(this)
			.datepicker("destroy")
			.datepicker({dateFormat:"dd MM yy"});
	});
	
	$(".app_content_caption").html(_this.title);
	$(_this.context).show({effect: "slide", direction:"left", duration: 800});
	form_set_toolbar(_this);
	_this._show();
}
function _form_base_hide(){
	$("#main-content-caption").html("");
	$(this.context).hide();//{effect:"slide", direction:"right", duration: 400});
	//:-( WORK-AROUND CAUSE I DON'T KNOW WHY IT MESSES UP MY FORM
	$("#ui-datepicker-div").hide();
	
}

function form_set_toolbar(_this){
	var toolbar = $(_this.context).find(".toolbar");
	
	toolbar.find("li a").each(function(){
		if ($(this).hasClass("btn-create")){
			$(this).button({icons:{primary:"ui-icon-plusthick"}});
		} else if ($(this).hasClass("btn-remove")){
			$(this).button({icons:{primary:"ui-icon-trash"}});
		} else if ($(this).hasClass("btn-update")){
			$(this).button({icons:{primary:"ui-icon-disk"}});
		} else if ($(this).hasClass("btn-refresh")){
			$(this).button({icons:{primary:"ui-icon-refresh"}});
		} else if ($(this).hasClass("btn-import")){
			$(this).button({icons:{primary:"ui-icon-copy"}});
		} else {
			$(this).button();
		}
		
	});

	toolbar.buttonset();
	toolbar.prop("parent_div", toolbar.parents("div").prop("id"));

	if ($(".app_toolbar").children().length != 0){
		//IF NOT EMPTY THEN GO PUT THE TOOLBAR BACK INTO THE PARENT CONTAINER/FORM
		var d = $($(".app_toolbar").children()[0]);
		$("#"+d.prop("parent_div")).append($(".app_toolbar").children());
	}
	
		
	$(".app_toolbar").append(toolbar);
	
	$(".app_toolbar .btn-create")
		.unbind("click")
		.click(function(){
			if (typeof _this.create == "function")
				_this.create();
		});
	$(".app_toolbar .btn-update")
		.unbind("click")
		.click(function(){
			if (typeof _this.update == "function")
				_this.update();
		});
	$(".app_toolbar .btn-remove")
		.unbind("click")
		.click(function(){
			if (typeof _this.remove == "function")
				_this.remove();
		});
	
	$(".app_toolbar .btn-refresh")
		.unbind("click")
		.click(function(){
			if (typeof _this.refresh == "function")
				_this.refresh();
		});
}

function _form_serialize(server_call){
	var _this = this;
	if (typeof server_call == "string") this.server_call = server_call;
	
	if (this.data == null || typeof this.data == "undefined") this.data = {};
	
	$(this.context).find("input,select,textarea").each(function(){
		if (this.type == "checkbox"){
			_this.data[this.id] = ($(this).is(":checked") ? 1 : 0);
			
		} else if (this.type == "select-multiple" && $(this).hasClass("form-field")){
			var key = this.id;
			_this.data[this.id] = [];
			$(this).find("option").each(function(){
				_this.data[key].push(escape($.trim($(this).val())));
			});
		} else {
			_this.data[this.id] = escape($.trim($(this).val()));
		}
	});
}

function _form_set_values(data){
	hide_title();
	var _this = this;
	if (typeof data == "undefined"){
		$(this.context).find(".form-field").each(function(){
			if (this.type == "checkbox"){
				this.checked = false;
			} else {
				$(this).val("");
			}
		});
	}
	$(data).each(function(){
		for(key in this){
			var value = this[key];
			var el = $(_this.context).find("#"+key);
			if (!el){
				continue;
			}
			
			if (el.attr("type") == "checkbox"){
				el.removeAttr("checked");
				if (value == 1)
					el.attr("checked", "checked");
			} else {
				el.val(unescape(value));
			}
		}
	});
	
	//FORMAT ALL DATE CORRECTLY
	//SET-UP THE DATE PICKERS
	$(_this.context).find(".form-date-field").each(function(){
		var date = Date.createFromFirebird($(this).val());
		if (date){
			$(this).val($.format.date(date, "dd MMMM yyyy"));
		} else {
			$(this).val("");
		}
	});

}

function _form_get_field(el){
	return $(this.context).find("#"+el);
}

function _set_check_box(el, value){
	if (typeof el == "string"){
		if (el.indexOf("#") != 0) el = "#"+el;
	}
	
	el = $(this.context).find(el);
	el.removeAttr("checked");
	
	if (typeof value == "undefined") 
		value = false;
	else if (typeof value == "number") 
		value = (value == 1);
	else if (typeof value == "string") 
		value = (value == "1");
	
	if (true == value)
		el.attr("checked", "checked");
}

function _build_data_dropdown(el, data, _default, fields, _first_entry_blank){
	if (!fields){
		fields = ["id", "name"];
	}
	if (!_first_entry_blank) _first_entry_blank = false;
	
	var lst = this.field(el);
	lst.children().remove();
	if (_first_entry_blank == true)
		lst.append("<option>");
		
	$(data).each(function(){
		var opt = $("<option value='"+this[fields[0]]+"'>"+unescape(this[fields[1]])+"</option>");
		if (_default && _default == this[fields[0]]) opt.attr("selected", "selected");
		lst.append(opt);
	});
}
function _build_number_dropdown(el, _default, _step, _max, _start){
	if (typeof el == "string"){
		if (el.indexOf("#") != 0) el = "#"+el;
	}
	if (!_start) _start = 0;
	
	el = $(this.context).find(el);
	el.children().remove();
	for(var key = _start; key <= _max; key += _step){
		
		var opt = $("<option value='"+key+"'>"+
					(key < 10 ? "0" : "") + 
					key
					+"</option>");
		if (key == _default) opt.attr("selected", "selected");
		el.append(opt);
	}
}
